//
//  LBSWebViewController.m
//  LBSYunDemo
//
//  Created by RetVal on 4/7/13.
//  Copyright (c) 2013 RetVal. All rights reserved.
//

#import "LBSWebViewController.h"
#import "iToast.h"
#import "LBSRental.h"
#import "LBSDataCenter.h"
#import "Constants.h"
@interface LBSWebViewController ()<UIWebViewDelegate, UIGestureRecognizerDelegate>
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;

@end

@implementation LBSWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webNavigationItem.title = LBSUIIndex0Title;
    CGRect frame = self.webView.frame;
    if ([[LBSDataCenter defaultCenter] isIphone5])
    {
        frame.size.height += 88;
        self.webView.frame = frame;
    }
	[self.webView setScalesPageToFit:YES];
	[self.webView setUserInteractionEnabled:NO];
	[self.webView setBackgroundColor:[UIColor clearColor]];
	[self.webView setDelegate:self];
	[self.webView setOpaque:YES];
	[self.webView loadRequest:[NSURLRequest requestWithURL:self.rental.roomURL]];
	 self.navigationController.navigationBar.translucent = NO;
    // send the PV data to the Baidu Server.. should I send it or not...
    
    NSURL * url = [NSURL URLWithString:@"http://api.map.baidu.com/images/blank.gif?t=92248538&platform=ios&logname=lbsyunduanzu"];
    NSMutableURLRequest *pvRequest = [NSMutableURLRequest requestWithURL:url];
    [pvRequest setHTTPMethod:@"GET"];
    [NSURLConnection sendAsynchronousRequest:pvRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        NSString *string = [NSString stringWithFormat:@"%@", data];
        NSLog(@"response is %d - %@", [httpResponse statusCode], string);
        if (error != nil)
        {
            NSLog(@"%@", error);
        }
        else
        {
            NSLog(@"send pv success.");
        }
    }];
    
	frame = CGRectMake(0, 44, 320, (480 + ([[LBSDataCenter defaultCenter] isIphone5] ? 88 : 0)));
	UIView *view = [[UIView alloc] initWithFrame:frame];
	[view setTag:117]; // 不能删
	[view setBackgroundColor:[UIColor blackColor]];
	[view setAlpha:0.8];
	[self.view addSubview:view];
	self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 44.0f, 32.0f, 32.0f)];
	[self.activityIndicator setCenter:view.center];
	[self.activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
	[view addSubview:self.activityIndicator];
	[self.view bringSubviewToFront:view];
}
- (IBAction)hitReturn:(UIBarButtonItem *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
		
	}];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark UIGestrueRecongniazerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.view];
    NSLog(@"handleSingleTap!pointx:%f,y:%f",point.x,point.y);
}

#pragma mark -
#pragma mark UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
	[self.webView setUserInteractionEnabled:NO];
	[self.activityIndicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	[self.webView setUserInteractionEnabled:YES];
	[self.activityIndicator stopAnimating];
	UIView *view = (UIView *)[self.view viewWithTag:117];
	[view removeFromSuperview];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
	[self.webView setUserInteractionEnabled:YES];
	[self.activityIndicator stopAnimating];
	NSLog(@"LBSWebView load failed TUT...%@", error);
	[iToast make:@"网页加载失败:(" duration:750];
}

@end
